C question
Hello list,
As far as I recall from K&R, isn't pointer arithmetic on a void pointer banned? And any effort to do that results in an error - because the compiler won't know by how much size to increment the pointer for a statement like "ptr++"? But then how about this:
[rick@linux rick]$ cat t.c
#include <stdio.h>
int main()
{
void *ptr = 0;
printf("%d \n", ptr+1);
}[rick@linux rick]$ gcc t.c
[rick@linux rick]$ ./a.out
1
[rick@linux rick]$
It compiles and runs fine ... !
TIA,
Rick
--
To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to [email protected] Please read the FAQ at http://kernelnewbies.org/FAQ
Quoted 6 lines · Rick Brown
Hello list,
As far as I recall from K&R, isn't pointer arithmetic on a void pointer banned? And any effort to do that results in an error - because the compiler won't know by how much size to increment the pointer for a statement like "ptr++"? But then how about this:
But in the program, you aren't actually trying to dereference the value. Just adding means it becomes normal arithmetic and that is why you get result as 1. You will see the error if you try to dereference it.
/tmp> gcc a.ca.c: In function ‘main’:
a.c:5: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘void *’ a.c:6: warning: dereferencing ‘void *’ pointer
a.c:6: error: invalid use of void expression
/tmp> cat a.c
#include <stdio.h>
int main()
{
void *ptr = 0;
printf("%d \n", ptr+1);
printf("%d \n", *(ptr+1));
}
Quoted 25 lines
[rick@linux rick]$ cat
t.c
#include <stdio.h>
intmain()
{
void *ptr = 0;
printf("%d \n", ptr+1);
}
[rick@linux rick]$ gcct.c
[rick@linux rick]$ ./a.out
1
[rick@linux rick]$It compiles and runs fine ... !
TIA,
Rick
--
To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to [email protected] Please read the FAQ at http://kernelnewbies.org/FAQ
Signature · 10 lines
Thanks - Manish ================================== [$\*.^ -- I miss being one of them ================================== -- To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to [email protected] Please read the FAQ at http://kernelnewbies.org/FAQ
Hi Rick,
gcc can warn about void pointer increment if you use compiler option -Wpointer-arith
==
Kalpesh
Quoted 63 lines · Manish Katiyar
Quoted 6 lines · Rick Brown
Hello list,
As far as I recall from K&R, isn't pointer arithmetic on a void pointer banned? And any effort to do that results in an error - because the compiler won't know by how much size to increment the pointer for a statement like "ptr++"? But then how about this:
But in the program, you aren't actually trying to dereference the value. Just adding means it becomes normal arithmetic and that is why you get result as 1. You will see the error if you try to dereference it.
/tmp> gcc
a.ca.c: In function ‘main’:
a.c:5: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘void *’ a.c:6: warning: dereferencing ‘void *’ pointer
a.c:6: error: invalid use of void expression
/tmp> cata.c
#include <stdio.h>
intmain()
{
void *ptr = 0;
printf("%d \n", ptr+1);
printf("%d \n", *(ptr+1));
}Quoted 15 lines
[rick@linux rick]$ cat
t.c
#include <stdio.h>
intmain()
{
void *ptr = 0;
printf("%d \n", ptr+1);
}
[rick@linux rick]$ gcct.c
[rick@linux rick]$ ./a.out
1
[rick@linux rick]$It compiles and runs fine ... !
Quoted 10 lines
TIA,
Rick
--
To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to [email protected] Please read the FAQ at http://kernelnewbies.org/FAQ--
Thanks -
Manish
==================================
[$\*.^ -- I miss being one of them
==================================
Quoted 6 lines
--
To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to [email protected] Please read the FAQ at http://kernelnewbies.org/FAQ
--
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Quoted 5 lines · Rick Brown
Quoted 4 lines
As far as I recall from K&R, isn't pointer arithmetic on a void pointer banned? And any effort to do that results in an error - because the compiler won't know by how much size to increment the pointer for a statement like "ptr++"?
Quoted 4 lines · Manish Katiyar
But in the program, you aren't actually trying to dereference the value. Just adding means it becomes normal arithmetic and that is why you get result as 1. You will see the error if you try to dereference it.
This comment is a bit misleading. The standard does not define behaviour of pointer arithmetic on pointer to void. What one need to realise is that undefined behaviour means compiler's documentation may well define how such a construct is evaluated and gcc (with proper options) decides to treat pointer to void as if sizeof(void) == 1.
So the thing it's true pointer arithmetic on a pointer to void is undefined behaviour as far as C standard is concerned however because Linux is compiled with gcc kernel's developers tend to make use of gcc's extensions and one of it is arithmetic on a pointer to void.
Signature · 10 lines
Best regards, _ _ .o. | Liege of Serenely Enlightened Majesty of o' \,=./ `o ..o | Computer Science, Michał "mina86" Nazarewicz (o o) ooo +---<[email protected]>---<[email protected]>---ooO--(_)--Ooo-- -- To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.linux-learn.org/faqs
Hi Rick,
Some days ago i had the same question in my mind. While going through "The Linux Kernel Architecture" book (by Wolfgang Mauerer), i got the answer:
The GNU compiler supports arithmetic with void pointers as well as function pointers. The increment step is 1 byte. These are used by the kernel at various points.
Have fun.
Regards
Mayur
Quoted 31 lines · Rick Brown
Hello list,
As far as I recall from K&R, isn't pointer arithmetic on a void pointer banned? And any effort to do that results in an error - because the compiler won't know by how much size to increment the pointer for a statement like "ptr++"? But then how about this:
[rick@linux rick]$ cat
t.c
#include <stdio.h>
intmain(){ void *ptr = 0; printf("%d \n", ptr+1); }[rick@linux rick]$ gcc
t.c
[rick@linux rick]$ ./a.out
1
[rick@linux rick]$It compiles and runs fine ... !
TIA,
Rick
--
To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to [email protected] Please read the FAQ at http://kernelnewbies.org/FAQ
Quoted 9 lines · mayur nande
Hi Rick,
Some days ago i had the same question in my mind. While going through "The Linux Kernel Architecture" book (by Wolfgang Mauerer), i got the answer:
The GNU compiler supports arithmetic with void pointers as well as function pointers. The increment step is 1 byte. These are used by the kernel at various points.
Quoted 39 lines
Have fun.
Regards
MayurQuoted 31 lines · Rick Brown
Hello list,
As far as I recall from K&R, isn't pointer arithmetic on a void pointer banned? And any effort to do that results in an error - because the compiler won't know by how much size to increment the pointer for a statement like "ptr++"? But then how about this:
[rick@linux rick]$ cat
t.c
#include <stdio.h>
intmain(){ void *ptr = 0; printf("%d \n", ptr+1); }[rick@linux rick]$ gcc
t.c
[rick@linux rick]$ ./a.out
1
[rick@linux rick]$It compiles and runs fine ... !
TIA,
Rick
--
To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to [email protected] Please read the FAQ at http://kernelnewbies.org/FAQ
Arithmetic on void and function pointers is part of GNU C extensions, C standard
does not support it (size of void and functions is taken as 1). There are many such
GNU extensions which are used in kernel. One way to figure out which all extensions
are used is by providing -pedantic flag, this will emit warning for such usage.
I think many of these extensions have became part of C99 standard already.
Regards,
Sandeep.